package cannot.serviceImpl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cannot.mapper.HistoryMapper;
import cannot.pojo.History;
import cannot.pojo.HistoryExample;
import cannot.pojo.Remark;
import cannot.pojo.RemarkExample;
import cannot.service.HistoryService;

@Service
public class HistoryServiceImpl implements HistoryService {

	@Autowired
	HistoryMapper historyMapper;
	@Override
	public void addHistory(History history) {
		historyMapper.insert(history);
	}
	@Override
	public void addHistoryByTrue(History history) {
		//判断数据库中是否已经有了该记录
		
		//有的话更新该记录
		HistoryExample example = new HistoryExample();
		example.createCriteria().andTv_idEqualTo(history.getTv_id()).andUidEqualTo(history.getUid());
		List<History> historys = historyMapper.selectByExample(example);
		if(historys!=null&&historys.size()!=0){
			History history1 = historys.get(0);
			history1.setCreateDate(new Date());
			historyMapper.updateByPrimaryKey(history1);
		}else{
			//没有的话增加记录
			historyMapper.insert(history);
		}
	}
	@Override
	public List<History> getByUid(Long id) {
		return historyMapper.geyByUid(id);
	}
	@Override
	public void deleteByTvId(Integer id) {
		HistoryExample example = new HistoryExample();
		example.createCriteria().andTv_idEqualTo(id);
		List<History> historys = historyMapper.selectByExample(example);
		for(History history:historys){
			historyMapper.deleteByPrimaryKey(history.getId());
		}
	}
}
